﻿/*
    框架首页JS
    author by mmcai
*/
var
        $nav = $("#nav"),
        $main = $("#main"),
        $navTab = $("#navTab"),
        $body = $("#body"),
        $maskLayer = $("#maskLayer");

/// <reference path="jquery.1.8.3.js" />
$(".tree").delegate("a", "click", function () {
    var $that = $(this);
    $(".tree").find("a").removeClass("selected");
    $that.addClass("selected");
});

//通过data-src获取侧边栏菜单
$(function () {
    var $navSrc = $nav.attr("data-src");
    if ($navSrc != "") {
        $.ajax({
            url: $navSrc,
            type: "GET",
            beforeSend: function () {
                $maskLayer.show();
            },
            success: function () {
                var data = arguments[0];
                if (data == "false") return;
                var _result = eval("(" + data + ")");

                var source = $("#menu-template").html();
                var template = Handlebars.compile(source);
                var navHtml = template(_result[0]);
                $("#nav").find("ul").html(navHtml);


                //带子菜单的折叠树
                $(".tree-item").each(function (i) {
                    var
                            $this = $(this),
                            $toggle = $this.find("a:first");

                    $toggle.bind("click", function (e) {

                        var
                                $that = $(this),
                                $menu = $that.next(".tree-menu");

                        if ($that.closest("li").hasClass("open")) {
                            $menu.hide();
                            $this.removeClass("open");
                            $that.removeClass("selected");
                            $that.find(".fa").removeClass("fa-folder-open-o").addClass("fa-folder-o");

                        } else {
                            $menu.show();
                            $this.addClass("open");
                            $(this).addClass("selected");
                            $that.find(".fa").removeClass("fa-folder-o").addClass("fa-folder-open-o");
                        }

                        e.stopPropagation();
                        e.preventDefault();
                    });
                });

                $maskLayer.hide();
            },
            error: function (msg) {
                console.log("请求服务器错误：" + msg);
            }
        });
    }


});

$dropdown = $(".dropdown");
$dropdown.each(function () {
    var
            $this = $(this),
            $toggle = $this.find(".drop-toggle"),
            $menu = $this.find(".drop-menu");

    $toggle.on("click", function (e) {

        if ($this.hasClass("open")) {
            $this.removeClass("open");
            $menu.hide();
        } else {
            $this.addClass("open");
            $menu.show();
        }

        e.stopPropagation();
        e.preventDefault();
    });

});

$(document).on("click", function () {
    if ($(".dropdown").hasClass("open")) {
        $(".dropdown").removeClass("open");
        $(".dropdown").find(".drop-menu").hide();
    }
});

//切换选项卡页面
$navTab.delegate("ul:first a", "click", function () {
    var
            $this = $(this),
            $id = $this.attr("data-id"),
            $index = $this.parent().index();

    $navTab.find("a").removeClass("active");
    $this.addClass("active");

    $navTab.find(".drop-nav a").removeClass("active");
    $navTab.find(".drop-nav a[data-id=" + $id + "]").addClass("active");

    //$nav.find(".tree").hide();
    //$nav.find(".tree").eq($index).show();

    //$main.find(".page-panel").hide();
    //$("#" + $id + "Panel").show();

    //加载左侧菜单
    //$.ajax({});
});
